﻿//We are given a matrix of strings of size N x M. Sequences in the matrix we define as sets of several neighbor elements located on the same line, column or diagonal. Write a program that finds the longest sequence of equal strings in the matrix. Example:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _03SequencesInStringMatrix
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter width of matrix M:");
            int m = int.Parse(Console.ReadLine());
            Console.Write("Enter depth of matrix N:");
            int n = int.Parse(Console.ReadLine());
            //string[,] matrix = 
            //{
            //{"ha", "fifi", "ho", "hi"},
            //{"fo", "ha", "hi", "xx"},
            //{"xxx", "ho", "ha", "xx"},
            //};
            string[,] matrix = new string[n, m];
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write("matrix[{0},{1}]:", i, j);
                    matrix[i, j] = Console.ReadLine();
                }
            }
            //Printing matrix: ****************************************************************************
            Console.WriteLine("The matrix you entered looks like this:");
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write("{0,2} ", matrix[i,j]);
                }
                Console.WriteLine();
            }
            int count = 1;
            int maxCount = 1;
            string element = "";
            int direction = 0;      // 1 = -, 2 = \, 3 = |, 4 = /  
            //Checking right:********************************************************************************
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1) - 1; j++)
                {
                    if (matrix[i,j] == matrix[i,j+1])
                    {
                        count++;
                    }
                    else
                    {
                        count = 1;
                    }
                    if (maxCount < count)
                    {
                        maxCount = count;
                        element = matrix[i, j];
                        direction = 1;
                    }
                }
                count = 1;
            }
            //Checking down:*********************************************************************************
            for (int i = 0; i < matrix.GetLength(1); i++)
            {
                for (int j = 0; j < matrix.GetLength(0) - 1; j++)
                {
                    if (matrix[j, i] == matrix[j + 1, i])
                    {
                        count++;
                    }
                    else
                    {
                        count = 1;
                    }
                    if (maxCount < count)
                    {
                        maxCount = count;
                        element = matrix[j, i];
                        direction = 3;
                    }
                }
                count = 1;
            }
            //Checking diagonal right \ :***********************************************************************

            for (int i = matrix.GetLength(0) - 2; i >= 0; i--)
            {
                for (int j = i, k = 0; j < matrix.GetLength(0) - 1 && k < matrix.GetLength(1) - 1; j++, k++)
                {
                    if (matrix[j, k] == matrix[j + 1, k + 1])
                    {
                        count++;
                    }
                    else
                    {
                        count = 1;
                    }
                    if (maxCount < count)
                    {
                        maxCount = count;
                        element = matrix[j, k];
                        direction = 2;
                    }
                }
                count = 1;
            }
            for (int i = matrix.GetLength(1) - 3; i >= 0; i--)
            {
                for (int j = 0, k = 1; j < matrix.GetLength(0) - 1 && k < matrix.GetLength(1) - 1; j++, k++)
                {
                    if (matrix[j, k] == matrix[j + 1, k + 1])
                    {
                        count++;
                    }
                    else
                    {
                        count = 1;
                    }
                    if (maxCount < count)
                    {
                        maxCount = count;
                        element = matrix[j, k];
                        direction = 2;
                    }
                }
                count = 1;
            }
            //Checking diginal left / :*************************************************************************
            for (int i = 1; i < matrix.GetLength(0); i++)
            {
                for (int j = 0, k = i; j < matrix.GetLength(0) && k >= 1; j++, k--)
                {
                    if (matrix[j, k] == matrix[j + 1, k - 1])
                    {
                        count++;
                    }
                    else
                    {
                        count = 1;
                    }
                    if (maxCount < count)
                    {
                        maxCount = count;
                        element = matrix[j, k];
                        direction = 4;
                    }
                }
                count = 1;
            }
            for (int i = 1; i < matrix.GetLength(0); i++)
            {
                for (int j = i, k = matrix.GetLength(1) - 1; j < matrix.GetLength(0) - 1 && k >= 1; j++, k--)
                {
                    if (matrix[j, k] == matrix[j + 1, k - 1])
                    {
                        count++;
                    }
                    else
                    {
                        count = 1;
                    }
                    if (maxCount < count)
                    {
                        maxCount = count;
                        element = matrix[j, k];
                        direction = 4;
                    }
                }
                count = 1;
            }
            //Printing result:*******************************************************************************
            if (direction == 1)
            {
                Console.WriteLine("Element {0} repeated {1} times horizontally.", element, maxCount);
            }
            if (direction == 2)
            {
                Console.WriteLine("Element {0} repeated {1} times diagonally \\ .", element, maxCount);
            }
            if (direction == 3)
            {
                Console.WriteLine("Element {0} repeated {1} times vertically.", element, maxCount);
            }
            if (direction == 4)
            {
                Console.WriteLine("Element {0} repeated {1} times diagonally / .", element, maxCount);
            }
            Console.WriteLine("The End!");
            Console.ReadLine();
        }
    }
}
